package com.xkcyy.demo.springcloud.simple.userclient.feign;

import org.apache.commons.io.FileUtils;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * UserFeign
 *
 * @author xkcyy
 * @date 2022/4/28
 **/
@FeignClient(url = "http://localhost:10030", name = "userservice")
public interface UserFeign {
    @PostMapping("/user/listusers")
    List<String> listUsers(@RequestParam("groupId") String groupId, @RequestBody Map<String, Object> values);

    @PostMapping(value = "/user/file",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    List<String> file(@RequestBody FileUploadCommand command);


    @PostMapping(value = "/user/file2",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    List<String> file2(@RequestParam("file2") MultipartFile file);


    class FileUploadCommand {
        private String uid;
        private String id;
        private String name;
        private long size;
        private long position;
        private String md5;
        private MultipartFile file;

        public String getUid() {
            return uid;
        }

        public void setUid(String uid) {
            this.uid = uid;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public long getSize() {
            return size;
        }

        public void setSize(long size) {
            this.size = size;
        }

        public long getPosition() {
            return position;
        }

        public void setPosition(long position) {
            this.position = position;
        }

        public String getMd5() {
            return md5;
        }

        public void setMd5(String md5) {
            this.md5 = md5;
        }

        public MultipartFile getFile() {
            return file;
        }

        public void setFile(MultipartFile file) {
            this.file = file;
        }
    }

    class FileMultipartFile implements MultipartFile{
        private File file;
        public FileMultipartFile(File file){
            this.file = file;
        }


        @Override
        public String getName() {
            return file.getName();
        }

        @Override
        public String getOriginalFilename() {
            return file.getName();
        }

        @Override
        public String getContentType() {
            return "text/html";
        }

        @Override
        public boolean isEmpty() {
            return false;
        }

        @Override
        public long getSize() {
            return file.length();
        }

        @Override
        public byte[] getBytes() throws IOException {
            try(FileInputStream stream  = new FileInputStream(this.file)){
                byte[] bytes = new byte[(int) this.file.length()];
                stream.read(bytes);
                return bytes;
            }
        }

        @Override
        public InputStream getInputStream() throws IOException {
            return new FileInputStream(this.file);
        }

        @Override
        public void transferTo(File dest) throws IOException, IllegalStateException {

        }
    }
}
